package com.loukou.lkt.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loukou.lkt.lkt.entity.SupplierGoods;
import com.loukou.lkt.lkt.enums.SettlementTypeEnum;
import com.loukou.lkt.lkt.req.PurchaseOrderReqDto;
import com.loukou.lkt.lkt.req.PurchaseOrderReturnReqDto;
import com.loukou.lkt.lkt.resp.PageModel;
import com.loukou.lkt.lkt.resp.PoPurchaseOrderDetailRespDto;
import com.loukou.lkt.lkt.resp.PoPurchaseOrderGoodsDto;
import com.loukou.lkt.lkt.resp.PoPurchaseOrderReturnDetailRespDto;
import com.loukou.lkt.lkt.resp.PurchaseGoodsHistoryPriceDto;
import com.loukou.lkt.lkt.resp.PurchaseOrderListDto;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.resp.SupplierGoodsInfoDto;
import com.loukou.lkt.lkt.resp.WarehouseDto;
import com.loukou.lkt.lkt.service.SupplierGoodsService;
import com.loukou.lkt.lkt.service.WarehouseService;
import com.loukou.lkt.web.annotation.AuthCheck;
import com.loukou.lkt.web.processor.PoPurchaseOrderProcessor;
import com.loukou.lkt.web.processor.PoPurchaseReturnProcessor;
import com.loukou.lkt.web.req.AddGoodsToPurchaseReqDto;
import com.loukou.lkt.web.req.SearchSupplierGoodsDto;
import com.loukou.lkt.web.resp.RespPureDto;
@Controller
@RequestMapping("/po_purchase")
public class PoPurchaseController extends BaseController{
	
	@Autowired
	PoPurchaseOrderProcessor poPurchaseOrderProcessor;
	@Autowired
	PoPurchaseReturnProcessor poPurchaseReturnProcessor;
	@Autowired
	WarehouseService warehouseService;
	@Autowired
	SupplierGoodsService supplierGoodsService;
	
	@RequestMapping("test1")
	@AuthCheck(privileges = {"module1.pri1","module2.pri1"})
	public String test1(){
		return "datatable";
	}
	
	@RequestMapping("test2")
	@AuthCheck(privileges = {"module1.pri1"}, isRedirect = true)
	public String test2(){
		return "";
	}
	
	@RequestMapping("example2")
	public String test3(){
		return "example";
	}
	
	@RequestMapping("purchaseOrderList")
	public String purchaseOrderList(@RequestParam String type, Model model){
		String action= "去进货";
		if(type.contains("-") ){
			action = "去退货";
		}
		model.addAttribute("type", action);
		return "purchase/purchaseOrderList";
	}
	@RequestMapping("purchaseOrderDetail")
	public String purchaseOrderDetail(@RequestParam int supplierId,@RequestParam String supplierName, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("supplierName", supplierName);
		model.addAttribute("supplierId", supplierId);
		model.addAttribute("warehouses", list);
		model.addAttribute("settlementMap", SettlementTypeEnum.getMap());
		return "purchase/purchaseOrderDetail";
	}
	@RequestMapping("getAllSuppliers")
	@ResponseBody
	public List<PurchaseOrderListDto> purchaseOrderList(){
		
		if(getAuthInfo() == null) {
			return null;
		}
		
		List<PurchaseOrderListDto> resp = poPurchaseOrderProcessor.
				getSupplierOrdersInfo(getAuthInfo().getId()).getResult();
		
//		//FIXME
//		PageModel<PurchaseOrderListDto> resp = new  PageModel<PurchaseOrderListDto>();
//				PurchaseOrderListDto purchaseOrderListDto = new PurchaseOrderListDto();
//				purchaseOrderListDto.setSupplierId(78);
//				purchaseOrderListDto.setReceivedNum(111);
//				purchaseOrderListDto.setSkuNum(122);
//				purchaseOrderListDto.setSupplierCode("222");
//				purchaseOrderListDto.setSupplierName("供应商啊啊啊aa");
//				purchaseOrderListDto.setTotalNum(22);
//				purchaseOrderListDto.setUnreceivedNum(222);
//				ArrayList<PurchaseOrderListDto> list = new ArrayList<PurchaseOrderListDto>();
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				list.add(purchaseOrderListDto);
//				resp.setData(list);
		return resp;
	}
	
	@RequestMapping("supplierCode")
	@ResponseBody
	public List<PurchaseOrderListDto> getBySupplierCode(@RequestParam String supplierCode){
		List<PurchaseOrderListDto> resp = poPurchaseOrderProcessor.
				getSupplierOrdersInfoBySupplierCode(supplierCode).getResult();
		
		//FIXME
//		resp = new  PageModel<PurchaseOrderListDto>();
//						PurchaseOrderListDto purchaseOrderListDto = new PurchaseOrderListDto();
//						purchaseOrderListDto.setSupplierId(78);
//						purchaseOrderListDto.setReceivedNum(111);
//						purchaseOrderListDto.setSkuNum(122);
//						purchaseOrderListDto.setSupplierCode("111");
//						purchaseOrderListDto.setSupplierName("supln mae");
//						purchaseOrderListDto.setTotalNum(22);
//						purchaseOrderListDto.setUnreceivedNum(222);
//						ArrayList<PurchaseOrderListDto> list = new ArrayList<PurchaseOrderListDto>();
//						list.add(purchaseOrderListDto);
//						resp.setRecordNum(1);
//						resp.setData(list);
						
		return resp;
	}
	
	@RequestMapping("supplierName")
	@ResponseBody
	public List<PurchaseOrderListDto> getBySupplierName(@RequestParam String supplierName){
		List<PurchaseOrderListDto> resp = poPurchaseOrderProcessor.
				getSupplierOrdersInfoBySupplierName(supplierName).getResult();
		
		//FIXME
//				resp = new  PageModel<PurchaseOrderListDto>();
//						PurchaseOrderListDto purchaseOrderListDto = new PurchaseOrderListDto();
//						purchaseOrderListDto.setReceivedNum(111);
//						purchaseOrderListDto.setSkuNum(122);
//						purchaseOrderListDto.setSupplierId(78);
//						purchaseOrderListDto.setSupplierCode("333");
//						purchaseOrderListDto.setSupplierName("supln mae");
//						purchaseOrderListDto.setTotalNum(22);
//						purchaseOrderListDto.setUnreceivedNum(222);
//						ArrayList<PurchaseOrderListDto> list = new ArrayList<PurchaseOrderListDto>();
//						list.add(purchaseOrderListDto);
//						resp.setRecordNum(1);
//						resp.setData(list);
						
		return resp;
	}
	
	@RequestMapping("supplierGoods")
	@ResponseBody
	public PageModel<SupplierGoodsInfoDto> getSupplierGoods(@RequestParam int warehouseId, @RequestParam int supplierId, 
			@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = false, defaultValue = "1000") int pageSize){
		PageModel<SupplierGoodsInfoDto> resp = supplierGoodsService.getSupplierGoodsInfoList(warehouseId, supplierId, pageNum, pageSize);
		return resp;
	}
	
	@RequestMapping("goods/purchaseHistory")
	@ResponseBody
	public List<SupplierGoodsInfoDto> getPurchaseHistory(@RequestParam int supplierId,@RequestParam int warehouseId,
			@RequestParam String taosku, @RequestParam String bn, 
			@RequestParam String goodsName, @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", required = false, defaultValue = "1000") int pageSize){
		List<SupplierGoodsInfoDto> resp = supplierGoodsService.getSupplierGoodsByKeywords(supplierId, taosku, bn, goodsName, warehouseId);
		//TODO
//		resp = new PageModel<PurchaseGoodsHistoryPriceDto>();
//		List<PurchaseGoodsHistoryPriceDto> list = new ArrayList<PurchaseGoodsHistoryPriceDto>();
//		PurchaseGoodsHistoryPriceDto purchaseGoodsHistoryPriceDto = new PurchaseGoodsHistoryPriceDto();
//		purchaseGoodsHistoryPriceDto.setBn("bn");
//		purchaseGoodsHistoryPriceDto.setTaosku("111");
//		purchaseGoodsHistoryPriceDto.setGoodsName("name");
//		list.add(purchaseGoodsHistoryPriceDto);
//		resp.setData(list);
		return resp;
	}
	
	@RequestMapping("purchaseReview")
	public String purchaseReview(){
		return "purchase/purchaseReview";
	}
	
	@RequestMapping("purchaseReturnReview")
	public String purchaseReturnReview(){
		return "purchase/purchaseReturnReview";
	}
	
	@RequestMapping("purchaseReturnOrderList")
	public String purchaseReturnOrderList(){
		return "purchase/purchaseReturnOrderList";
	}
	
	@RequestMapping("purchaseReturnOrderDetail")
	public String purchaseReturnOrderDetail(){
		return "purchase/purchaseReturnOrderDetail";
	}
	
	@RequestMapping(value="purchaseReturnOrderDetail",consumes="application/json")
	@ResponseBody
	public RespPureDto getSupplierGoodsByKeyWords(@RequestBody SearchSupplierGoodsDto req){
		RespPureDto respDto = new RespPureDto(200, "");
        return respDto;
	}
	
	@RequestMapping(value="viewPage")
	public String viewPurchaseOrderPage(@RequestParam String orderId, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("warehouses", list);
		model.addAttribute("settlementMap", SettlementTypeEnum.getMap());
        return "purchase/purchaseOrderView";
	}
	@RequestMapping(value="editPage")
	public String editPurchaseOrderPage(@RequestParam String orderId, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("settlementMap", SettlementTypeEnum.getMap());
		model.addAttribute("warehouses", list);
        return "purchase/purchaseOrderEdit";
	}
	@RequestMapping(value="editSubmit")
	public RespDto<String> submitEditPurchaseOrder(@RequestBody PurchaseOrderReqDto reqDto){
		RespDto<String> respDto = poPurchaseOrderProcessor.submitPurchaseOrder(reqDto);
        return respDto;
	}
	@RequestMapping(value="view")
	@ResponseBody
	public PoPurchaseOrderDetailRespDto viewPurchaseOrder(@RequestParam String orderId, Model model){
		PoPurchaseOrderDetailRespDto result = poPurchaseOrderProcessor.getPurchaseOrderDetail(orderId);
//		PoPurchaseOrderDetailRespDto result = new PoPurchaseOrderDetailRespDto();
//		result.setSupplierName("tset");
//		result.setWarehouseId(3);
//		result.setWarehouseName("test");
//		List<PoPurchaseOrderGoodsDto> orderGoodsDtoList = new ArrayList<PoPurchaseOrderGoodsDto>();
//		PoPurchaseOrderGoodsDto poPurchaseOrderGoodsDto = new PoPurchaseOrderGoodsDto();
//		poPurchaseOrderGoodsDto.setGoodsId(111);
//		poPurchaseOrderGoodsDto.setGoodsName("dafasdf");
//		poPurchaseOrderGoodsDto.setTaosku("d");
//		orderGoodsDtoList.add(poPurchaseOrderGoodsDto);
//		result.setOrderGoodsDtoList(orderGoodsDtoList);
        return result;
	}
	@RequestMapping(value="returnViewPage")
	public String returnViewPurchaseOrderPage(@RequestParam String orderId, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("warehouses", list);
        return "purchase/purchaseReturnOrderView";
	}
	@RequestMapping(value="returnEditPage")
	public String returnEditPurchaseOrderPage(@RequestParam String orderId, Model model){
		List<WarehouseDto> list = warehouseService.getAllWarehouseInfo();
		model.addAttribute("warehouses", list);
        return "purchase/purchaseReturnOrderEdit";
	}
	@RequestMapping(value="returnView")
	@ResponseBody
	public PoPurchaseOrderReturnDetailRespDto returnViewPurchaseOrder(String orderCode){
		PoPurchaseOrderReturnDetailRespDto  poPurchaseOrderReturnDetailRespDto =  poPurchaseReturnProcessor.getReturnOrderDetail(orderCode); 
        return poPurchaseOrderReturnDetailRespDto;
	}
	@RequestMapping(value="returnEditSubmit")
	@ResponseBody
	public RespDto<String> editReturnOrder(@RequestBody PurchaseOrderReturnReqDto req){
		RespDto<String> respDto = poPurchaseReturnProcessor.editReturnOrder(req);
        return respDto;
	}
	@RequestMapping(value="submitPurchaseOrder",consumes="application/json")
	@ResponseBody
	public RespDto<String> submitPurchaseOrder(@RequestBody PurchaseOrderReqDto req){
		RespDto<String> result = poPurchaseOrderProcessor.submitPurchaseOrder(req);
//		RespDto<String> result = new RespDto<String>();
        return result;
	}
	@RequestMapping(value="addGoodsToPurchaseOrder",method=RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public List<PoPurchaseOrderGoodsDto> addGoodsToPurchaseOrder(@RequestBody AddGoodsToPurchaseReqDto req){
		List<String> taosku = req.getTaosku();
		int supplierId = req.getSupplierId();
		int warehouseId = req.getWarehouseId();
		List<PoPurchaseOrderGoodsDto> result = poPurchaseOrderProcessor.addGoodsToPurchaseOrder(taosku, supplierId, warehouseId);
		//
//		List<PoPurchaseOrderGoodsDto> result = new ArrayList<PoPurchaseOrderGoodsDto>();
//		PoPurchaseOrderGoodsDto poPurchaseOrderGoodsDto = new PoPurchaseOrderGoodsDto();
//		poPurchaseOrderGoodsDto.setBn("dddd");
//		poPurchaseOrderGoodsDto.setGoodsName("test");
//		result.add(poPurchaseOrderGoodsDto);
//		result.add(poPurchaseOrderGoodsDto);
//		result.add(poPurchaseOrderGoodsDto);
        return result;
	}
}
